package com.company;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

//自定义一个classLoader demo
//classLoader是一个抽象类
public class MyClassLoader extends ClassLoader{
    private String path;
    private String classLoaderName;

    public MyClassLoader(String path,String classLoaderName)
    {
        this.path=path;
        this.classLoaderName=classLoaderName;
    }

    //用于寻找类文件
    @Override
    public Class findClass(String name)
    {
        byte[] b=loadClassData(name);
        return defineClass(name,b,0,b.length);
    }

    //用于加载类文件 返回一个字节数组
    private byte[] loadClassData(String name)
    {
        name=path+name+".class";
        InputStream in=null;
        ByteArrayOutputStream out=null;
        try{
            in = new FileInputStream(new File(name));
            out = new ByteArrayOutputStream();
            int i=0;
            while( (i=in.read())!=-1 )
            {
                out.write(i);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                out.close();
                in.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return out.toByteArray();
    }
}
